package com.xw.tools;

import com.xw.tools.factory.WinFactory;
import com.xw.tools.view.AbstractWin;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.List;

public class Main extends Application {


    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("coderTools");

        //StackPane root = new StackPane();
        //root.getChildren().add(btn);

        Scene scene = new Scene(new VBox(), 800, 495);
        initView(scene, primaryStage);
        primaryStage.setScene(scene);
        primaryStage.show();

//        Stage stage = new Stage();
//        stage.initModality(Modality.WINDOW_MODAL);
//        stage.initOwner(primaryStage);
//        Group root = new Group();
//        Scene scene2 = new Scene(root, 300, 250, Color.LIGHTGREEN);
//        stage.setScene(scene2);
//        stage.show();
    }

    private void initView(Scene scene, Stage stage) {
        setMenus(scene, stage);
    }

    private void setMenus(Scene scene, Stage stage) {
        MenuBar menuBar = new MenuBar();

        List<String> menuNames = WinFactory.getNames();
        for (String menuName : menuNames) {
            Menu menuTool = new Menu(menuName);
            List<String> itemNames = WinFactory.getItemNames(menuName);
            for (String itemName : itemNames) {
                MenuItem item = new MenuItem(itemName);
                item.setOnAction((ActionEvent t) -> {
                    AbstractWin view = null;
                    try {
                        view = (AbstractWin) WinFactory.get(menuName, itemName).newInstance();
                        view.show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                menuTool.getItems().add(item);
            }
            menuBar.getMenus().add(menuTool);
        }
        Menu menuHelp = new Menu("Help");
        MenuItem about = new MenuItem("About");
        about.setOnAction((ActionEvent t) -> {
            Alert _alert = new Alert(Alert.AlertType.INFORMATION, "关于我们");
            _alert.setTitle("关于");
//            _alert.setHeaderText("aaa");
//            _alert.setContentText("bbbb");
            _alert.initOwner(stage);
            _alert.show();
        });
        menuHelp.getItems().add(about);
        menuBar.getMenus().add(menuHelp);
        VBox box = (VBox) scene.getRoot();
        box.getChildren().add(menuBar);
    }
}
